﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace DigitsRecognizer
{
    public class LogOnForm : Form
    {
        //private String Login, Password;
        private Label LoginLabel, PasswordLabel;
        private TextBox LoginTB, PasswordTB;
        private Button LogOnButton, CancelButton, CreateButton;
        private LogOnHandler LogOnHandler;
        private List<object> Result;

        public delegate void PassControl(object sender);    // delegat stworzony do obsługi przesyłu danych
        public event PassControl passControl;               // zdarzenie polegające na przesłaniu danych pomiędzy formularzami

        public LogOnForm()
        {
            LogOnHandler = new LogOnHandler();
            Result = new List<object>();
            this.Height = 220;
            this.Width = 220;
            this.CenterToScreen();
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.Text = "Logowanie";

            LoginLabel = new Label();
            LoginLabel.AutoSize = true;
            LoginLabel.Text = "Login:";
            LoginLabel.Left = 20;
            LoginLabel.Top = 35;

            PasswordLabel = new Label();
            PasswordLabel.AutoSize = true;
            PasswordLabel.Text = "Hasło:";
            PasswordLabel.Left = 17;
            PasswordLabel.Top = 75;

            LoginTB = new TextBox();
            LoginTB.Text = "";
            LoginTB.Left = 70;
            LoginTB.Top = 33;

            PasswordTB = new TextBox();
            PasswordTB.Text = "";
            PasswordTB.Left = 70;
            PasswordTB.Top = 73;
            PasswordTB.PasswordChar = '*';

            LogOnButton = new Button();
            LogOnButton.Text = "Zaloguj";
            LogOnButton.Left = 3;
            LogOnButton.Top = 140;
            LogOnButton.Width = 50;

            CreateButton = new Button();
            CreateButton.Text = "Utwórz konto";
            CreateButton.Left = 56;
            CreateButton.Top = 140;
            CreateButton.Width = 78;

            CancelButton = new Button();
            CancelButton.Text = "Zakończ";
            CancelButton.Left = 137;
            CancelButton.Top = 140;
            CancelButton.Width = 60;

            this.Controls.Add(LoginLabel);
            this.Controls.Add(PasswordLabel);
            this.Controls.Add(LoginTB);
            this.Controls.Add(PasswordTB);
            this.Controls.Add(LogOnButton);
            this.Controls.Add(CreateButton);
            this.Controls.Add(CancelButton);

            /* obsługa wciśnięcia przycisku Cancel */
            EventHandler cancelButtonEH = new EventHandler(onClickCancel);
            CancelButton.Click += cancelButtonEH;

            /* obsługa wciśnięcia przycisku Create */
            EventHandler createButtonEH = new EventHandler(onClickCreate);
            CreateButton.Click += createButtonEH;

            /* obsługa wciśnięcia przycisku LogOn */
            EventHandler logOnButtonEH = new EventHandler(onClickLogOn);
            LogOnButton.Click += logOnButtonEH;

            FormClosedEventHandler applicationExit = new FormClosedEventHandler(LogOnForm_FormClosed);
            this.FormClosed += applicationExit;
        }

        public void LogOnForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoginTB.Text = "-1";
            PassData();
        }

        public void onClickCancel(object sender, EventArgs args)
        {
            LoginTB.Text = "-1";
            PassData();
            this.Close();
        }

        public void onClickCreate(object sender, EventArgs args)
        {
            if (LoginTB.Text != "" && PasswordTB.Text != "")
            {
                Result = LogOnHandler.CreateAccount(LoginTB.Text.Trim(), PasswordTB.Text.Trim());
                MessageBox.Show((string)Result[0], (string)Result[1], (MessageBoxButtons)Result[2], (MessageBoxIcon)Result[3]);
                if ((MessageBoxIcon)Result[3] == MessageBoxIcon.Information)
                {
                    PassData();
                    this.Hide();
                }
            }
            else
            {
                MessageBox.Show("Podaj poprawne login i hasło!", "Niepoprawne login/hasło", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void onClickLogOn(object sender, EventArgs args)
        {
            Result = LogOnHandler.CheckUserInDB(LoginTB.Text.Trim(), PasswordTB.Text.Trim());
            if(Result.Count > 0)
            {
                MessageBox.Show((string)Result[0], (string)Result[1], (MessageBoxButtons)Result[2], (MessageBoxIcon)Result[3]);
            }
            else
            {
                PassData();
                this.Hide();
            }
        }

        private void PassData() 
        {
            /* przekazanie jako argumentu danych (userName, radioOption) w postaci tablicy String */
            String[] str = { LoginTB.Text };

            // uruchomienie zdarzenia
            passControl(str);
        }
    }
}
